package com.pig4cloud.pig.device.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import com.pig4cloud.pig.device.entity.TpStationInfoEntity;
import com.pig4cloud.pig.device.service.TpStationInfoService;

import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import com.pig4cloud.pig.common.security.annotation.HasPermission;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 收费站车道信息台账
 *
 * @author LiaoJinghang
 * @date 2025-11-06 10:08:38
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/tpStationInfo" )
@Tag(description = "tpStationInfo" , name = "收费站车道信息台账管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class TpStationInfoController {

    private final  TpStationInfoService tpStationInfoService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param tpStationInfo 收费站车道信息台账
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @HasPermission("device_tpStationInfo_view")
    public R getTpStationInfoPage(@ParameterObject Page page, @ParameterObject TpStationInfoEntity tpStationInfo) {
        LambdaQueryWrapper<TpStationInfoEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(StrUtil.isNotBlank(tpStationInfo.getCompanyName()),TpStationInfoEntity::getCompanyName,tpStationInfo.getCompanyName());
		wrapper.like(StrUtil.isNotBlank(tpStationInfo.getStationName()),TpStationInfoEntity::getStationName,tpStationInfo.getStationName());
		wrapper.eq(StrUtil.isNotBlank(tpStationInfo.getLaneExitEntrance()),TpStationInfoEntity::getLaneExitEntrance,tpStationInfo.getLaneExitEntrance());
		wrapper.eq(StrUtil.isNotBlank(tpStationInfo.getLaneType()),TpStationInfoEntity::getLaneType,tpStationInfo.getLaneType());
        return R.ok(tpStationInfoService.page(page, wrapper));
    }


    /**
     * 通过条件查询收费站车道信息台账
     * @param tpStationInfo 查询条件
     * @return R  对象列表
     */
    @Operation(summary = "通过条件查询" , description = "通过条件查询对象" )
    @GetMapping("/details" )
    @HasPermission("device_tpStationInfo_view")
    public R getDetails(@ParameterObject TpStationInfoEntity tpStationInfo) {
        return R.ok(tpStationInfoService.list(Wrappers.query(tpStationInfo)));
    }

    /**
     * 新增收费站车道信息台账
     * @param tpStationInfo 收费站车道信息台账
     * @return R
     */
    @Operation(summary = "新增收费站车道信息台账" , description = "新增收费站车道信息台账" )
    @SysLog("新增收费站车道信息台账" )
    @PostMapping
    @HasPermission("device_tpStationInfo_add")
    public R save(@RequestBody TpStationInfoEntity tpStationInfo) {
        return R.ok(tpStationInfoService.save(tpStationInfo));
    }

    /**
     * 修改收费站车道信息台账
     * @param tpStationInfo 收费站车道信息台账
     * @return R
     */
    @Operation(summary = "修改收费站车道信息台账" , description = "修改收费站车道信息台账" )
    @SysLog("修改收费站车道信息台账" )
    @PutMapping
    @HasPermission("device_tpStationInfo_edit")
    public R updateById(@RequestBody TpStationInfoEntity tpStationInfo) {
        return R.ok(tpStationInfoService.updateById(tpStationInfo));
    }

    /**
     * 通过id删除收费站车道信息台账
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除收费站车道信息台账" , description = "通过id删除收费站车道信息台账" )
    @SysLog("通过id删除收费站车道信息台账" )
    @DeleteMapping
    @HasPermission("device_tpStationInfo_del")
    public R removeById(@RequestBody String[] ids) {
        return R.ok(tpStationInfoService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param tpStationInfo 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @HasPermission("device_tpStationInfo_export")
    public List<TpStationInfoEntity> exportExcel(TpStationInfoEntity tpStationInfo,String[] ids) {
        return tpStationInfoService.list(Wrappers.lambdaQuery(tpStationInfo).in(ArrayUtil.isNotEmpty(ids), TpStationInfoEntity::getId, ids));
    }

    /**
     * 导入excel 表
     * @param tpStationInfoList 对象实体列表
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @HasPermission("device_tpStationInfo_export")
    public R importExcel(@RequestExcel List<TpStationInfoEntity> tpStationInfoList, BindingResult bindingResult) {
        return R.ok(tpStationInfoService.saveBatch(tpStationInfoList));
    }
}
